package com.tracesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tracesystem.commons.Result;
import com.tracesystem.controller.form.ChangeMemberForm;
import com.tracesystem.controller.form.NewMemberForm;
import com.tracesystem.controller.form.RemMemberForm;
import com.tracesystem.model.IssueProjectRoleRel;
import com.tracesystem.dao.IssueProjectRoleRelDao;
import com.tracesystem.service.IssueProjectRoleRelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tracesystem.util.WoniuCopyUtil;
import com.tracesystem.util.exception.ProjectRoleRelException;
import com.tracesystem.util.exception.code.IssueProjectRoleRelExceptionCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@Service
public class IssueProjectRoleRelServiceImpl extends ServiceImpl<IssueProjectRoleRelDao, IssueProjectRoleRel> implements IssueProjectRoleRelService {
    @Resource
    private IssueProjectRoleRelDao issueProjectRoleRelDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> addMember(NewMemberForm newMemberForm) {
        boolean save = save(WoniuCopyUtil.copyOne(newMemberForm, IssueProjectRoleRel.class));
        if (save) {
            return Result.getInstance().setMsg("添加成员成功");
        } else {
            throw new ProjectRoleRelException(IssueProjectRoleRelExceptionCode.IssueProjectRoleRel_ADD_ERROR);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> fulshMember(ChangeMemberForm changeMemberForm) {
        QueryWrapper<IssueProjectRoleRel> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq("PROJECT_ID", changeMemberForm.getProjectId()).eq("USER_ID", changeMemberForm.getUserId());
        int update = issueProjectRoleRelDao.update(WoniuCopyUtil.copyOne(changeMemberForm, IssueProjectRoleRel.class), updateWrapper);
        if (update > 0) {
            return Result.getInstance().setMsg("修改项目用户成功");
        } else {
            throw new ProjectRoleRelException(IssueProjectRoleRelExceptionCode.ssueProjectRoleRel_UPDATA_ERROR);
        }


    }


}
